<template>
    <div class="content">
        <div class="login-content">
            <a class="banner-box"></a>
            <div class="login-box">
                <h1>后台管理系统</h1>
                <el-form ref="ruleFormRef" :model="ruleForm" :rules="rules" size="large">
                    <el-form-item prop="userName">
                        <el-input v-model="ruleForm.userName" placeholder="username" />
                    </el-form-item>
                    <el-form-item prop="password">
                        <el-input type="password" v-model="ruleForm.password" placeholder="password" />
                    </el-form-item>
                    <el-form-item>
                        <el-button class="login-btn" type="primary" @click="submitForm(ruleFormRef)">
                            Login
                        </el-button>
                    </el-form-item>
                </el-form>
            </div>
        </div>
    </div>
</template>

<script setup>
import { reactive, ref } from 'vue'
import { loginAPI } from '@/api/index.js';
import { useRouter } from 'vue-router'
import { useUserGlobalStore } from '@/store/UserGobalStore'

const $router = useRouter()

const ruleForm = reactive({
    userName: '',
    password: ''
})
const rules = reactive({
    userName: [
        { required: true, message: 'Please input username', trigger: 'blur' },
    ],
    password: [
        { required: true, message: 'Please input password', trigger: 'blur' },
    ],
})
const ruleFormRef = ref(null)

const submitForm = () => {
    console.log("ruleForm===", ruleForm)
    ruleFormRef.value.validate(async (valid) => {
        if (valid) {
            let res = await loginAPI({
                userName: ruleForm.userName,
                password: ruleForm.password
            })
            localStorage.setItem('token', res.token)
            const store = useUserGlobalStore()
            await store.getClobalUserInfo()
            //跳转到首页
            $router.push({
                path: '/home',
            }).catch(err => { })
        }
    });
}
</script>

<style lang="scss" scoped>
.content {
    background: url("@/assets/image/bag.png") no-repeat;
    background-size: cover;
    height: 100vh;
    width: 100vw;
    background-position: center;
    display: flex;
    justify-content: center;
    align-items: center;
}

.login-content {
    position: relative;
    height: 380px;
    width: 800px;
    background: rgba(208, 211, 212);
    border-radius: 30px;
    padding: 30px;
}

.banner-box {
    display: block;
    width: 500px;
    height: 100%;
    background: url("@/assets/image/bag.png") no-repeat;
    background-size: cover;
    border-radius: 20px 0 0 20px;

}

.login-box {
    width: 270px;
    height: 100%;
    position: absolute;
    top: 0;
    right: 0;
    border-radius: 0 30px 30px 0;
    display: flex;
    flex-direction: column;
    align-items: center;
}

.login-box h1 {
    font-size: 30px;
    margin: 50px 0;
    color: #fff;
}


.login-btn {
    width: 200px;
    height: 40px;
    margin-top: 10px;
    border-radius: 40px;
    font-size: 18px;
    background-color: rgba(174, 182, 191);
    color: #fff;
    border: none;
    outline: none;
}

@media screen and (max-width: 900px) {

    .login-content {
        margin: 0 20px;
    }

    .banner-box {
        border-radius: 20px;
    }

    .login-box {
        background: rgba(208, 211, 212, 0.5);
    }
}

@media screen and (max-width: 600px) {
    .login-content {
        background: rgba(208, 211, 212, 0);
    }

    .banner-box {
        display: none;
    }

    .login-box {
        position: initial;
        margin: -30px auto;
        background: rgba(208, 211, 212, 0);
    }
}
</style>